package com.guazai.project.controller;

import com.guazai.common.pojo.InterfaceInfoProportion;
import com.guazai.project.annotation.AuthCheck;
import com.guazai.project.common.BaseResponse;
import com.guazai.project.common.ResultUtils;
import com.guazai.project.constant.UserConstant;
import com.guazai.project.model.vo.*;
import com.guazai.project.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 分析控制器
 *
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @Resource
    private UserService userService;

    @Resource
    private InterfaceLogService interfaceLogService;

    @Resource
    private InterfaceLogWeekCountService interfaceLogWeekCountService;

    @Resource
    private InterfaceInfoProportionService interfaceInfoProportionService;

    /**
     * 获得最热门的一些接口
     * @return
     */
    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<AnalysisVO>> listTopInvokeInterfaceInfo() {
        return ResultUtils.success(interfaceInfoService.getTopInterfaceInfo());
    }

    /**
     * 获得热门接口
     * @return
     */
    @GetMapping("/topInterfaceInfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<TopInterfaceInfoVO> getTopInterfaceInfo() {
        return ResultUtils.success(interfaceInfoService.getTop());
    }

    /**
     * 获得接口调用平均时间
     * @return
     */
    @GetMapping("/interface/introduceRow")
    public BaseResponse<IntroduceRowVO> getInterfaceIntroduceRow() {
        // 获取接口调用次数
        Long count = interfaceInfoService.getInterfaceInfoTotalInvokesCount();
        // 获取最近接口调用平均时间
        Integer cost = interfaceLogService.getInterfaceInfoAverageCost();

        IntroduceRowVO vo = new IntroduceRowVO();
        vo.setInterfaceInfoCount(count);
        vo.setCost(cost);

        return ResultUtils.success(vo);
    }

    @GetMapping("/introduceRow")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<IntroduceRowVO> getIntroduceRow(HttpServletRequest request) {

        IntroduceRowVO introduceRowVO = new IntroduceRowVO();
        // 获取接口调用次数
        Long interfaceInfoCount = interfaceInfoService.getInterfaceInfoTotalInvokesCount();
        // 获取最近接口调用平均时间
        Integer cost = interfaceLogService.getInterfaceInfoAverageCost();
        introduceRowVO.setInterfaceInfoCount(interfaceInfoCount);
        introduceRowVO.setCost(cost);

        return ResultUtils.success(introduceRowVO);
    }

    /**
     * 获得接口调用次数占比
     * @return
     */
    @GetMapping("/interfaceinfoProportion")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<ProportionSalesVO> getInterfaceinfoProportion() {
        ProportionSalesVO proportionSalesVO = new ProportionSalesVO();
        List<InterfaceInfoProportion> interfaceInfoProportionList = interfaceInfoProportionService.list();
        proportionSalesVO.setInterfaceInfoProportionList(interfaceInfoProportionList);
        return ResultUtils.success(proportionSalesVO);
    }

    @GetMapping("/salesCard")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<SalesCardVO> getSalesCard(){
        SalesCardVO salesCardVO = new SalesCardVO();
        salesCardVO.setPayoutRank(new ArrayList<>());
        salesCardVO.setTotalDay(new ArrayList<>());
        return ResultUtils.success(salesCardVO);
    }
}
